<?php
namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Service\BlackWord;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Basic;
use App\Facades\AdminC;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Input;


class BasicController extends Controller
{
    use ModelForm;

    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('基本设置');
            $content->description('参数设置');
            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return  AdminC::grid(Basic::class,function(Grid $grid){
            $grid->filter(function($filter){
                $model = new Basic();
                $filter->like('name','参数名称');
                $filter->equal('status','状态')->select($model->status_arr);
            });
            $grid->id('Id')->sortable()->style('width:10%;');
            $grid->name('参数名')->style('width:15%;');
            $grid->keyword('关键字')->style('width:10%;');
            $grid->val('参数')->style('width:10%;');
            $grid->value('特殊参数')->style('width:25%;');
            $grid->status('状态')->display(function($status){
                return $this->status_arr[$status];
            })->style('width:10%;');
            $grid->created_at('创建时间')->style('width:10%;');

            $grid->disableExport();
        });
    }

    public function create()
    {
        return AdminC::content(function(Content $content){
            $content->header('参数设置');
            $content->description('添加');
            $content->body($this->form());
        });
    }

    public function edit($id)
    {
        return AdminC::content(function(Content $content) use($id) {
            $content->header('参数设置');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    public function form()
    {
        return AdminC::form(Basic::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('name','参数名')->rules("required");
            $form->text('keyword','关键字')->rules("required");
            $form->text('val','参数');
            $form->textarea('value','特殊文本参数');
            $form->select('status', '状态')->options($form->model()->status_arr);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }

    /**
     * 敏感词
     */
    public function blackWord()
    {
        $keyword = [];
        $f = fopen(base_path().'/storage/dict.txt','r');
        while(! feof($f)) {
            $item = trim(fgets($f));
            if (empty($item)) {
                continue;
            }
            $keyword[] = $item;
        }
        fclose($f);

        return view('admin.basic_blackword', ['keyword' => implode('|',$keyword)]);
    }

    /**
     * 保存铭感词
     */
    public function blackWordSave()
    {
        $input = Input::all();
        try {
            $keyword = explode('|', $input['keyword']);
        }catch (\Exception $e) {
            $keyword = $input['keyword'];
        }
        $f = fopen(base_path().'/storage/dict.txt','w');
        flock($f, LOCK_EX);
        foreach ($keyword as $v) {
            fwrite($f, $v."\r\n");
        }
        flock($f, LOCK_UN);
        fclose($f);

        $res = (new BlackWord())->createDict(base_path().'/storage/dict.txt');
        if($res) {
            admin_toastr('保存成功');

            return redirect('/admin/blackword');
        }else{
            admin_toastr('保存失败');

            return redirect('/admin/blackword');
        }
    }

}